package br.ufpr.inf.escel.brain.variator;

import br.ufpr.inf.escel.consts.Constantes;
import java.io.File;
import java.util.Random;

/** 
 * The main variato r class. Reads the command line arguments,
 * instantiates the population, the file manager and the state machine,
 * initializes (state = 0) and starts the state machine.
 * 
 * @author Tamara Ulrich
 * @version 1.0
 */
public class Variator {

    static Populacao population;
    static FileManager fileManager;
    static StateMachine stateMachine;
    public static Random randomGenerator;
    
    static {
        population = new Populacao();
        randomGenerator = new Random();
        stateMachine = new StateMachine();
        fileManager = new FileManager();
    }

    /** Simple constructor. Is never needed,
     * because the Variator class has only static
     * fields and methods.
     */
    public Variator() {
    }

    /** Reads the command line arguments, initializes (state = 0)
     * and starts the state machine */
    public static void main(String[] args) {

        File local = new File("");
        String pisaPath = local.getAbsolutePath() + "/pisa";
        
        String lotz = pisaPath + "/lotz2_param.txt";
        String pisa = pisaPath + "/PISA_";

        String version = "0.2";

        System.out.println("Variator started");

        // set some static variables
        population.paramFileName = lotz;
        population.commFilePath = pisa;
        population.pollingInterval = (int) Math.round(1000 *
                Double.parseDouble(version));

        // set the communication files
        population.selectorFile = pisa + "sel";
        population.variatorFile = pisa + "var";
        population.archiveFile = pisa + "arc";
        population.initialFile = pisa + "ini";
        population.stateFile = pisa + "sta";
        population.configFile = pisa + "cfg";

        fileManager.restart(Constantes.NOVA_PASTA_CUT);
//         set the state to 0 (initial state)
        fileManager.writeState(0);

        // start the state machine
        stateMachine.runStateMachine();

        debugPrint("Variator terminated.");
    }

    /**
     * Writes the given string to System.out
     * if the parameter <code>Population.debugPrint</code> is set to <code>true</code>.
     * @param text The text which will be printed to System.out
     */
    static void debugPrint(String text) {
        if (population.debugPrint) {
            System.out.println(text);
        }
    }
}
